
package com.wyzz.hr.action;

import java.io.File;
import java.util.*;
import org.apache.commons.lang.StringUtils;

import com.wyzz.hr.base.BaseAction;
import com.wyzz.hr.base.DWRUtil;
import com.wyzz.hr.base.FileOperate;
import com.wyzz.hr.bo.IEmpTrainHisBo;
import com.wyzz.hr.pojo.Emphistorytrain;
import com.wyzz.hr.spring.SpringBeanFactory;

public class EmpTrainHisDWR extends BaseAction {

	public EmpTrainHisDWR() {
	}

	public String trainHisAdd() throws Exception {
		if ("self".equals(emphistorytrain.getEmployee().getId()))
			emphistorytrain.setEmployee(getCurrentEmp());
		if ("SUB".equals(super.authorityCondition)
				&& !checkAuth(emphistorytrain.getEmployee().getId())) {
			addActionError("\u60A8\u6CA1\u6709\u589E\u52A0\u6743\u9650\u6267\u884C\u672C\u64CD\u4F5C\uFF01");
			return "error";
		}
		empTrainHisBo = (IEmpTrainHisBo) SpringBeanFactory
				.getBean("empTrainHisBo");
		if (fileFileName != null && !"".equals(fileFileName)) {
			String UploadResult;
			String pathConfig = "sys.profile.file.path";
			String typeConfig = "sys.profile.file.type";
			String lengthConfig = "sys.profile.file.length";
			String postfix = fileFileName.substring(fileFileName
					.lastIndexOf("."));
			fileFileName = (new StringBuilder()).append(UUID.randomUUID())
					.append(postfix).toString();
			UploadResult = FileOperate.buildFile(file, pathConfig, typeConfig,
					lengthConfig, new String[] { fileFileName });
			if ("property".equals(UploadResult)) {
				addActionError("\u6587\u4EF6\u7684\u4E0A\u4F20\u9519\u8BEF\uFF01");
				return "error";
			}
			if ("fileExtendNameError".equals(UploadResult)) {
				addActionError("\u6587\u4EF6\u7684\u540E\u7F00\u540D\u4E0D\u5408\u6CD5\uFF01");
				return "error";
			}
			try {
				emphistorytrain.setEhtAttatchment(fileFileName);
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		emphistorytrain.setEmployee(emphistorytrain.getEmployee());
		emphistorytrain.setEhtCreateBy(getCurrentEmpNo());
		emphistorytrain.setEhtCreateDate(new Date());
		emphistorytrain.setEhtLastChangeBy(getCurrentEmpNo());
		emphistorytrain.setEhtLastChangeTime(new Date());
		empTrainHisBo.insert(emphistorytrain);
		addSuccessInfo("\u65B0\u589E\u57F9\u8BAD\u7ECF\u5386\u6210\u529F\uFF01");
		return "success";
	}

	public String trainHisDel(String ehjId) {
		String auth = DWRUtil.checkAuth("emptrainhisdwr", "");
		if ("error".equalsIgnoreCase(auth))
			return "noauth";
		empTrainHisBo = (IEmpTrainHisBo) SpringBeanFactory
				.getBean("empTrainHisBo");
		Emphistorytrain trainSearch = new Emphistorytrain();
		trainSearch.setEhtId(ehjId);
		List list = empTrainHisBo.search(trainSearch);
		Emphistorytrain EmpHT = (Emphistorytrain) list.get(0);
		if ("SUB".equals(auth) && !checkAuth(EmpHT.getEmployee().getId())) {
			return "noauth";
		} else {
			empTrainHisBo.deleteAttach(ehjId, EmpHT.getEhtAttatchment());
			empTrainHisBo.delete(ehjId);
			return "success";
		}
	}

	public String trainHisUpdate() throws Exception {
		Emphistorytrain empOldHT;
		empTrainHisBo = (IEmpTrainHisBo) SpringBeanFactory
				.getBean("empTrainHisBo");
		List list = empTrainHisBo.search(emphistorytrain);
		if (list.size() == 0)
			return "error";
		empOldHT = (Emphistorytrain) list.get(0);
		if ("SUB".equals(super.authorityCondition)
				&& !checkAuth(empOldHT.getEmployee().getId())) {
			addActionError("\u60A8\u6CA1\u6709\u4FEE\u6539\u6743\u9650\u6267\u884C\u672C\u64CD\u4F5C\uFF01");
			return "error";
		}
		if (fileFileName == null || "".equals(fileFileName))
			return null;
		String UploadResult;
		String pathConfig = "sys.profile.file.path";
		String typeConfig = "sys.profile.file.type";
		String lengthConfig = "sys.profile.file.length";
		String postfix = fileFileName.substring(fileFileName.lastIndexOf("."));
		fileFileName = (new StringBuilder()).append(UUID.randomUUID()).append(
				postfix).toString();
		UploadResult = FileOperate.buildFile(file, pathConfig, typeConfig,
				lengthConfig, new String[] { fileFileName,
						empOldHT.getEhtAttatchment() });
		if ("property".equals(UploadResult)) {
			addActionError("\u6587\u4EF6\u7684\u4E0A\u4F20\u9519\u8BEF\uFF01");
			return "error";
		}
		if ("fileExtendNameError".equals(UploadResult)) {
			addActionError("\u6587\u4EF6\u7684\u540E\u7F00\u540D\u4E0D\u5408\u6CD5\uFF01");
			return "error";
		}
		try {
			empOldHT.setEhtAttatchment(fileFileName);
		} catch (Exception e) {
			e.printStackTrace();
		}
		empOldHT.setEhtStartDate(emphistorytrain.getEhtStartDate());
		empOldHT.setEhtEndDate(emphistorytrain.getEhtEndDate());
		empOldHT.setEhtCourse(emphistorytrain.getEhtCourse());
		empOldHT.setEhtLocation(emphistorytrain.getEhtLocation());
		empOldHT.setEhtDepartment(emphistorytrain.getEhtDepartment());
		empOldHT.setEhtCertificate(emphistorytrain.getEhtCertificate());
		empOldHT.setEhtComments(emphistorytrain.getEhtComments());
		emphistorytrain.setEhtLastChangeBy(getCurrentEmpNo());
		emphistorytrain.setEhtLastChangeTime(new Date());
		empTrainHisBo.update(empOldHT);
		addSuccessInfo("\u66F4\u65B0\u57F9\u8BAD\u7ECF\u5386\u6210\u529F\uFF01");
		return "success";
	}

	public String attachDelete() throws Exception {
		empTrainHisBo = (IEmpTrainHisBo) SpringBeanFactory
				.getBean("empTrainHisBo");
		String fetches[] = { Emphistorytrain.PROP_EMPLOYEE };
		Emphistorytrain empOldHT = empTrainHisBo.load(ehtId, fetches);
		if ("SUB".equals(super.authorityCondition)
				&& !checkAuth(empOldHT.getEmployee().getId())) {
			addActionError("\u60A8\u6CA1\u6709\u5220\u9664\u6743\u9650\u6267\u884C\u672C\u64CD\u4F5C\uFF01");
			return "error";
		}
		if (ehtId == null || ehtId.equals("") || fileFileName == null
				|| "".equals(fileFileName)) {
			addActionError("\u53C2\u6570\u4F20\u9012\u9519\u8BEF\uFF01");
			return "error";
		}
		empTrainHisBo = (IEmpTrainHisBo) SpringBeanFactory
				.getBean("empTrainHisBo");
		if (!empTrainHisBo.deleteAttach(ehtId, fileFileName)) {
			addActionError("\u9644\u4EF6\u5220\u9664\u5931\u8D25\uFF01");
			return "error";
		} else {
			addActionMessage("\u9644\u4EF6\u5220\u9664\u6210\u529F\u3002");
			return "success";
		}
	}

	public Emphistorytrain loadTrainHis(String ehtId) {
		if (StringUtils.isEmpty(ehtId)) {
			return null;
		} else {
			empTrainHisBo = (IEmpTrainHisBo) SpringBeanFactory
					.getBean("empTrainHisBo");
			Emphistorytrain result = empTrainHisBo.load(ehtId, new String[0]);
			return result;
		}
	}

	public Emphistorytrain getEmphistorytrain() {
		return emphistorytrain;
	}

	public void setEmphistorytrain(Emphistorytrain emphistorytrain) {
		this.emphistorytrain = emphistorytrain;
	}

	public String getEhtId() {
		return ehtId;
	}

	public void setEhtId(String ehtId) {
		this.ehtId = ehtId;
	}

	public File getFile() {
		return file;
	}

	public void setFile(File file) {
		this.file = file;
	}

	public String getFileFileName() {
		return fileFileName;
	}

	public void setFileFileName(String fileFileName) {
		this.fileFileName = fileFileName;
	}

	private static final long serialVersionUID = 1L;
	private Emphistorytrain emphistorytrain;
	private String ehtId;
	private File file;
	private String fileFileName;
	private IEmpTrainHisBo empTrainHisBo;
}
